
$(document).ready(function(){
	
	$("#btnQuery").bind('click', function(){
		var beginDate=trim($("#qryOrderBeginDate").val());
		var endDate=trim($("#qryOrderEndDate").val());
		if(beginDate==""){
			showCfmMsg(getLanMsg("0ec22230-100070"), getLanMsg("0ec22230-100071"));
			return;
		}
		if(endDate==""){
			showCfmMsg(getLanMsg("0ec22230-100070"), getLanMsg("0ec22230-100072"));
			return;
		}
		var tempBeginDate=new Date(beginDate.replace(/\-/g, "\/"));
		var tempEndDate=new Date(endDate.replace(/\-/g, "\/"));
		if(tempBeginDate>tempEndDate){
			showCfmMsg(getLanMsg("0ec22230-100070"), getLanMsg("0ec22230-100073"));
			return ;
		}
		$("#qryForm").attr("action", ebvp_context+"/order/qryConfirmlist");
		$("#qryForm").submit();
	});
	
	// 接受订单
	$("#acceptOrder").bind('click', function(){
		var row=0;
		$("body").find("input:checked").each(function(){
			var pkOrder = $(this).attr("id");
			var status=trim($("#status_" + pkOrder).val());
			if(status==1){
				row=row+1;
				$("#pk_"+pkOrder).val(pkOrder);
				$("#status_"+pkOrder).val(2);
			}
		});
		
		if(row==0){
			showCfmMsg(getLanMsg("0ec22230-100070"), getLanMsg("0ec22230-100074"));
			return ;
		}else{
        	$("#listForm").attr("action", ebvp_context + "/order/acceptOrder");
        	$("#listForm").submit();
		}
	});
	
	// 拒绝订单
	$("#rejectOrder").bind('click', function(){
    	var row=0;
    	$("body").find("input:checked").each(function(){
			var pkOrder = $(this).attr("id");
			var status=trim($("#status_" + pkOrder).val());
			if(status==1){
				row=row+1;
				$("#pk_"+pkOrder).val(pkOrder);
			}
		});
		if(row==0){
			showCfmMsg(getLanMsg("0ec22230-100070"), getLanMsg("0ec22230-100074"));
			return false;
		}else{
			var reason = "";
			if("simpchn" == strSelLan){
    			window.lang = "simpchn";
			}else if("english" == strSelLan){
    			window.lang = "english";
			}else if("tradchn" == strSelLan){
				window.lang = "tradchn";
			}else if("french" == strSelLan){
				window.lang = "french";
			}
			var option = {
					title : getLanMsg("0ec22230-100075"),
					content : "<div style='margin-left:40px'><textarea id='rejectReasonPrompt' rows='3' cols='40' title='"+getLanMsg("0ec22230-100076")+"' class='textfield pd_5x2px' style='width:430px'></textarea><div id='errorMsg' style='width:430px;color:red'></div></div>",
					isHtmlContent:true,
					confirm : function() {
						reason = trim($("#rejectReasonPrompt").val());
						if(reason != null && reason != ""){
							$("#rejectReason").val(reason);
				        	$("#listForm").attr("action", ebvp_context + "/order/rejectOrder");
				        	$("#listForm").submit();
						}else{
							$("#errorMsg").text(getLanMsg("0ec22230-100077"));
							return false;
						}
					},
					cancel : function(){
					}
			};
			showConfirmDialog(option);
		}
    });
	
    //63近发版时提的一个Bug:NCdp204854023
	//关于确认数量，由于存在退货订单，数量为负数的情况，且需要与排程确认规则一致；
	//则：校验确认订单数量时，需要考虑，如果订单数量为负数，则确认订单数量要求<=0；如果订单数量为正数，则确认订单数量要求>=0
	function validateRequiredEle(){
		$("tbody").find(".error").remove();
		var sd002Value = $($("[name='sd002Value']")[0]).val();
		$("[name='confirmNum']").each(function(){
			var confirmNum = $(this).val();
			var orderNum = $($(this).parents("tr").find("[name='orderNum']")[0]).val();
			var ErrMsg = getLanMsg("0ec22230-100088");
			if(!confirmNum){                     //判空
				var errLabel = $("<img></img>").addClass("error").attr("for", "confirmNum").attr("title", ErrMsg)
					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png").attr("style", "position:absolute, left:10");
				errLabel.insertAfter($(this));
			}else if(!isValidNum(confirmNum)){   //判断数字有效性
				var ErrMsg = getLanMsg("0ec22230-100097");
				var errLabel = $("<img></img>").addClass("error").attr("for", "confirmNum").attr("title", ErrMsg)
					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png").attr("style", "position:absolute, left:10");
				errLabel.insertAfter($(this));
			}else if(orderNum>=0 && confirmNum<0){//当订单数量大于等于0，确认数量小于0时
				var ErrMsg = getLanMsg("0ec22230-100098");
				var errLabel = $("<img></img>").addClass("error").attr("for", "confirmNum").attr("title", ErrMsg)
					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png").attr("style", "position:absolute, left:10");
				errLabel.insertAfter($(this));
			}else if(orderNum<=0 && confirmNum>0){//当订单数量小于等于0，确认数量大于0时
				var ErrMsg = getLanMsg("0ec22230-100099");
				var errLabel = $("<img></img>").addClass("error").attr("for", "confirmNum").attr("title", ErrMsg)
					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png").attr("style", "position:absolute, left:10");
				errLabel.insertAfter($(this));
			}else{ //数量输入没问题时，再校验SD002参数，只有订单数量为>=0时SD002才起作用
				   //根据SD002参数，校验确认数量超出订单数量的大小
				   //港华需求：如果 confirmNum > (1+ SD002/100)*orderNum，则不允许保存或提交
				if(orderNum>=0){
					if(sd002Value != null && sd002Value !="null" && sd002Value !=""){
						var ErrMsg = getLanMsg("0ec22230-100094"); //"确认数量不能超出订单数量";
						if(sd002Value != 0){
							ErrMsg += getLanMsg("0ec22230-100095")+sd002Value + "%";
						}
						if(confirmNum*1 > (1+sd002Value*1/100)*orderNum){
							var errLabel = $("<img></img>").addClass("error").attr("for", "confirmNum").attr("title", ErrMsg)
							.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png").attr("style", "position:absolute, left:10");
							errLabel.insertAfter($(this));
							return false;
						}
					}
				}
			}
		});	
		//由于港华要求只能对数量进行确认，其他如单价和到货日期都不允许修改，故也不需要校验
		//所以去掉校验，不要影响提交
//		$("[name='confirmPrice']").each(function(){
//			var confirmPrice = $(this).val();
//			var ErrMsg = getLanMsg("0ec22230-100088");
//			if(!confirmPrice){
//				var errLabel = $("<img></img>").addClass("error").attr("for", "confirmPrice").attr("title", ErrMsg)
//					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png").attr("style", "position:absolute");
//				errLabel.insertAfter($(this));
//			}else if(!isValidNum(confirmPrice)||confirmPrice<0){
//				var ErrMsg = getLanMsg("0ec22230-100092");
//				var errLabel = $("<img></img>").addClass("error").attr("for", "confirmNum").attr("title", ErrMsg)
//					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png").attr("style", "position:absolute, left:10");
//				errLabel.insertAfter($(this));
//			}
//		});
//		$("[name='confirmArrDate']").each(function(){
//			var confirmArrDate = $(this).val();
//			var ErrMsg = getLanMsg("0ec22230-100088");
//			if(!confirmArrDate){
////				var errLabel = $("<img></img>").addClass("error").attr("for", "confirmArrDate").attr("title", ErrMsg)
////					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png").attr("style", "position:absolute");
////				errLabel.insertAfter($(this));
//			}else{
//				var orderDate = $("#Text8").val();
//				if(orderDate){
//					var vOrdDate = orderDate.replace("-", "/").replace("-", "/");
//					var vConDate = confirmArrDate.replace("-", "/").replace("-", "/");
//					var date1 = new Date(Date.parse(vOrdDate));
//					var date2 = new Date(Date.parse(vConDate));
//					if(date2 < date1){    //确认计划日期早于订单日期时，抛错
//						var dateErrMsg = getLanMsg("0ec22230-100090");
//						var errLabel = $("<img></img>").addClass("error").attr("for", "confirmArrDate").attr("title", dateErrMsg)
//							.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png").attr("style", "position:absolute");
//						errLabel.insertAfter($(this));
//					}
//				}
//			}
//		});
		
		if($("tbody").find(".error").length != 0){
			return false;
		}
		return true;
	}
	
	
	function isValidNum(inputVal){
		if(isNaN(inputVal)){
			return false;
		}else{
			return true;
		}
	}
	
	function isEqualsZero(inputVal){
		if(inputVal){
			if(inputVal=="0" || inputVal=="0.0" || inputVal=="0.00" || inputVal=="0.000" 
				||inputVal=="0.0000" || inputVal=="0.00000" || inputVal=="0.000000" || inputVal=="0.0000000" || inputVal=="0.00000000" 
				){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	
	// 提交变更订单
	$("#commitUpOrder").bind('click', function(){
		var row=0;
    	$("body").find("input:checked").each(function(){
			var pkOrder = $(this).attr("id");
			var status=trim($("#status_" + pkOrder).val());
			if(status==4){
				row=row+1;
				$("#pk_"+pkOrder).val($("#checkBox_"+pkOrder).val());
			}
		});
		if(row==0){
			showCfmMsg(getLanMsg("0ec22230-100070"), getLanMsg("0ec22230-100078"));
			return ;
		}else{
			if(!validateRequiredEle()){
				return ;
			}
        	$("#listForm").attr("action", ebvp_context + "/order/commitUptOrder");
        	$("#listForm").submit();
		}
    });
	
	$("#chkAll").bind('click', function(){
		if($("#chkAll").attr("checked") == "checked"){
			$("[name='checkBox']").each(function(){
				$(this).attr("checked", true);
			});
		}else{
			$("[name='checkBox']").each(function(){
				$(this).attr("checked", false);
			});
		}
	});
	
	$("[name='checkBox']").bind('click', function(){
		var bChkBoxes = $("[name='checkBox']");
		for(var i=0; i<bChkBoxes.size(); i++){
			if( bChkBoxes[i].checked == false){
				$("#chkAll").attr("checked", false);
				return;
			}
		}
		$("#chkAll").attr("checked", true);
	});
	
	
//	$("#confirmBrowse_goback").bind('click', function(){
//		//location.href = ebvp_context + "/order/goBack2OrderConfirm";
//		var woije=$("#woije").val();
//		location.href = ebvp_context + "/order/goBack2OrderConfirm?woije="+woije+"&responseStatusz="+5;
//	});
	
	$("#confirmBrowse_goback").bind('click', function(){
		//location.href = ebvp_context + "/order/goBack2OrderConfirm";
		var responseStatusds=$("#responseStatus").val();
		var woije=$("#woije").val();
		location.href = ebvp_context + "/order/goBack2OrderConfirm?woije="+woije+"&responseStatusz="+responseStatusds;
	});
	
	$("#confirmDetail_goback").bind('click', function(){
		var responseStatusds=$("#responseStatus").val();
		 var woije=$("#woije").val();
		location.href = ebvp_context + "/order/goBack2OrderConfirm?woije="+woije+"&responseStatusz="+responseStatusds;
	});
	
	$("#confirmDetail_save").bind('click', function(){
		if(!validateRequiredEle()){
			return ;
		}
	    //$("#orderForm").attr("action", ebvp_context + "/order/saveOrder");
	    //$("#orderForm").submit();
		var formStr1 = "";
		formStr1 += ("pkWebOrder="+$("#pkWebOrder").val());
		formStr1 += ("&pkOrder="+$("#pkOrder").val());
		formStr1 += ("&ts="+$("#ts").val());
		formStr1 += ("&pkPurCorp="+$("#pkPurCorp").val());
		formStr1 += ("&pkPurDept="+$("#pkPurDept").val());
		formStr1 += ("&pkPurPsn="+$("#pkPurPsn").val());
		formStr1 += ("&pkSupSendAddr="+$("#pkSupSendAddr").val());
		formStr1 += ("&pkInvoiceSupp="+$("#pkInvoiceSupp").val());
		formStr1 += ("&pkProtocol="+$("#pkProtocol").val());
		formStr1 += ("&sd002Value="+$("#sd002Value").val());
		formStr1 += ("&billNo=" + $($("[name='billNo']")[0]).val());
		formStr1 += ("&purCorpName=" + escape(encodeURIComponent($($("[name='purCorpName']")[0]).val())));
		formStr1 += ("&purDeptName=" + escape(encodeURIComponent($($("[name='purDeptName']")[0]).val())));
		formStr1 += ("&purPsnName=" + escape(encodeURIComponent($($("[name='purPsnName']")[0]).val())));
		formStr1 += ("&ordeDate=" + $($("[name='ordeDate']")[0]).val());
		formStr1 += ("&woije=" + $($("[name='woije']")[0]).val());
		formStr1 += ("&responseStatus=" + $($("[name='responseStatus']")[0]).val());
		formStr1 += ("&supSendAddrName=" + escape(encodeURIComponent($($("[name='supSendAddrName']")[0]).val())));
		formStr1 += ("&invoiceSuppName=" + escape(encodeURIComponent($($("[name='invoiceSuppName']")[0]).val())));
		formStr1 += ("&protocolName=" + escape(encodeURIComponent($($("[name='protocolName']")[0]).val())));
		formStr1 += ("&memo=" + escape(encodeURIComponent($($("[name='memo']")[0]).val())));  
		//以上是表头
		//以下是表体
		$("[name='pkItemOrder']").each(function(){
			
			var trNode = $(this).parents("tr");
			
			var pkItemOrderVal = $(trNode.find("[name='pkItemOrder']")[0]).val();
			var pkWebItemOrderVal = $(trNode.find("[name='pkWebItemOrder']")[0]).val();
			var contractNoVal = $(trNode.find("[name='contractNo']")[0]).val();
			var pkProdVal = $(trNode.find("[name='pkProd']")[0]).val();
			var prodNameVal = $(trNode.find("[name='prodName']")[0]).val();
			var moduleVal = $(trNode.find("[name='module']")[0]).val();
			var typeVal = $(trNode.find("[name='type']")[0]).val();
			var descVal = $(trNode.find("[name='desc']")[0]).val();
			var batchNoVal = $(trNode.find("[name='batchNo']")[0]).val();
			var pkMeasureVal = $(trNode.find("[name='pkMeasure']")[0]).val();
			var measureNameVal = $(trNode.find("[name='measureName']")[0]).val();
			var orderNumVal = $(trNode.find("[name='orderNum']")[0]).val();
			var orderPriceVal = $(trNode.find("[name='orderPrice']")[0]).val();
			var discountVal = $(trNode.find("[name='discount']")[0]).val();
			var netTaxPriceVal = $(trNode.find("[name='netTaxPrice']")[0]).val();
			var priceTaxTotalVal = $(trNode.find("[name='priceTaxTotal']")[0]).val();
			var rateVal = $(trNode.find("[name='rate']")[0]).val();
			var taxNumVal = $(trNode.find("[name='taxNum']")[0]).val();
			var pkCurrencyVal = $(trNode.find("[name='pkCurrency']")[0]).val();
			var curNameVal = $(trNode.find("[name='curName']")[0]).val();
			var planArrDateVal = $(trNode.find("[name='planArrDate']")[0]).val();
			var pkTransferTypeVal = $(trNode.find("[name='pkTransferType']")[0]).val();
			var transferNameVal = $(trNode.find("[name='transferName']")[0]).val();
			var pkReceiveAddrVal = $(trNode.find("[name='pkReceiveAddr']")[0]).val();
			var receiveAddrVal = $(trNode.find("[name='receiveAddr']")[0]).val();
			var pkProducerVal = $(trNode.find("[name='pkProducer']")[0]).val();
			var produceFactoryVal = $(trNode.find("[name='produceFactory']")[0]).val();
			var pkQulityLevelVal = $(trNode.find("[name='pkQulityLevel']")[0]).val();
			var qulityLevelVal = $(trNode.find("[name='qulityLevel']")[0]).val();
			var taxPriceVal = $(trNode.find("[name='taxPrice']")[0]).val();
			var numScaleVal = $(trNode.find("[name='numScale']")[0]).val();
			var priceScaleVal = $(trNode.find("[name='priceScale']")[0]).val();
			var amountScaleVal = $(trNode.find("[name='amountScale']")[0]).val();
			var maintaxfreepriceVal = $(trNode.find("[name='maintaxfreeprice']")[0]).val();
			var maintaxpriceVal = $(trNode.find("[name='maintaxprice']")[0]).val();
			var confirmPriceVal = $(trNode.find("[name='confirmPrice']")[0]).val();
			var confirmNumVal = $(trNode.find("[name='confirmNum']")[0]).val();
			var planArrDateddVal = $(trNode.find("[name='planArrDatedd']")[0]).val();
			var confirmArrDateVal = $(trNode.find("[name='confirmArrDate']")[0]).val();
			
			var tempStr = "&pkItemOrder=" + pkItemOrderVal
						  +"%26" + pkWebItemOrderVal 
						  +"%26" + contractNoVal
			              +"%26" + pkProdVal 
			              +"%26" + "" 
			              +"%26" + "" 
			              +"%26" + "" 
			              +"%26" + descVal 
			              +"%26" + batchNoVal 
			              +"%26" + pkMeasureVal 
			              +"%26" + measureNameVal 
			              +"%26" + orderNumVal 
			              +"%26" + orderPriceVal 
			              +"%26" + discountVal 
			              +"%26" + netTaxPriceVal 
			              +"%26" + priceTaxTotalVal 
			              +"%26" + rateVal 
			              +"%26" + taxNumVal 
			              +"%26" + pkCurrencyVal 
			              +"%26" + curNameVal 
			              +"%26" + planArrDateVal 
			              +"%26" + pkTransferTypeVal 
			              +"%26" + transferNameVal 
			              +"%26" + pkReceiveAddrVal 
			              +"%26" + receiveAddrVal 
			              +"%26" + pkProducerVal 
			              +"%26" + produceFactoryVal 
			              +"%26" + pkQulityLevelVal 
			              +"%26" + qulityLevelVal 
			              +"%26" + taxPriceVal 
			              +"%26" + numScaleVal 
			              +"%26" + priceScaleVal 
			              +"%26" + amountScaleVal 
			              +"%26" + maintaxfreepriceVal 
			              +"%26" + maintaxpriceVal 
			              +"%26" + confirmPriceVal
			              +"%26" + confirmNumVal
			              +"%26" + planArrDateddVal
			              +"%26" + confirmArrDateVal;
			              
			formStr1 += tempStr;
		});
		
		//ajax提交
		var bodyContent = $.ajax( {
			url : ebvp_context + "/order/saveOrder",
			type : "POST",
			data : formStr1,
			dataType : "text",
			async : false,
			success : function(responseText) {
				$("body").find(".block1").remove();
				if(responseText!= null && responseText.indexOf("-@BUSIERROR@-") != -1){
					if(responseText.indexOf("TS_CHECKERROR") != -1){
						responseText = "-@BUSIERROR@-" + "数据已修改，请刷新页面重新操作";
					}
					var spanObj = $("<span></span>").attr("style", "float:right; color:red;").html(responseText.substring(13,responseText.length));
					var imgObj = $("<img></img>").attr("style", "float:right;").attr("src", "/ebvp/commonres/common/frame/src/img/img_error.png");
					var div = $("<div></div>").addClass("block1").attr("style","float:right;");
					div.append(spanObj).append(imgObj);
					div.insertAfter($(".block2"));
				}else if(responseText!= null && responseText.indexOf("-@SYSERROR@-") != -1){
					var spanObj = $("<span></span>").attr("style", "float:right; color:red;").html(responseText.substring(12,responseText.length));
					var imgObj = $("<img></img>").attr("style", "float:right;").attr("src", "/ebvp/commonres/common/frame/src/img/img_error.png");
					var div = $("<div></div>").addClass("block1").attr("style","float:right;");
					div.append(spanObj).append(imgObj);
					div.insertAfter($(".block2"));
				}else if(responseText!= null && responseText.indexOf("-@SUCCESS@-") != -1){
					//这里刷新响应状态 
					$("#responseStatus").val("4");
					$("#responseStatus").next("input").val(getLanMsg("0ec22230-100028"));
					var spanObj = $("<span></span>").attr("style", "float:right; color:red;").html(getLanMsg("0ec22230-100084"));
					var imgObj = $("<img></img>").attr("style", "float:right;").attr("src", "/ebvp/commonres/common/frame/src/img/img_success.png");
					var div = $("<div></div>").addClass("block1").attr("style","float:right;");
					div.append(spanObj).append(imgObj);
					div.insertAfter($(".block2"));
				}
			}
		})
	});


	// 提交
	$("#confirmDetail_submit").bind('click', function(){
		if(!validateRequiredEle()){
			return ;
		}
		
		var formStr1 = "";
		formStr1 += ("pkWebOrder="+$("#pkWebOrder").val());
		formStr1 += ("&pkOrder="+$("#pkOrder").val());
		formStr1 += ("&ts="+$("#ts").val());
		formStr1 += ("&pkPurCorp="+$("#pkPurCorp").val());
		formStr1 += ("&pkPurDept="+$("#pkPurDept").val());
		formStr1 += ("&pkPurPsn="+$("#pkPurPsn").val());
		formStr1 += ("&pkSupSendAddr="+$("#pkSupSendAddr").val());
		formStr1 += ("&pkInvoiceSupp="+$("#pkInvoiceSupp").val());
		formStr1 += ("&pkProtocol="+$("#pkProtocol").val());
		formStr1 += ("&sd002Value="+$("#sd002Value").val());
		formStr1 += ("&billNo=" + $($("[name='billNo']")[0]).val());
		formStr1 += ("&purCorpName=" + escape(encodeURIComponent($($("[name='purCorpName']")[0]).val())));
		formStr1 += ("&purDeptName=" + escape(encodeURIComponent($($("[name='purDeptName']")[0]).val())));
		formStr1 += ("&purPsnName=" + escape(encodeURIComponent($($("[name='purPsnName']")[0]).val())));
		formStr1 += ("&ordeDate=" + $($("[name='ordeDate']")[0]).val());
		formStr1 += ("&woije=" + $($("[name='woije']")[0]).val());
		formStr1 += ("&responseStatus=" + $($("[name='responseStatus']")[0]).val());
		formStr1 += ("&supSendAddrName=" + escape(encodeURIComponent($($("[name='supSendAddrName']")[0]).val())));
		formStr1 += ("&invoiceSuppName=" + escape(encodeURIComponent($($("[name='invoiceSuppName']")[0]).val())));
		formStr1 += ("&protocolName=" + escape(encodeURIComponent($($("[name='protocolName']")[0]).val())));
		formStr1 += ("&memo=" + escape(encodeURIComponent($($("[name='memo']")[0]).val()))); 
		//以上是表头
		//以下是表体
		var index= 0;
		$("[name='pkItemOrder']").each(function(){
			
			index++;
			var trNode = $(this).parents("tr");
			
			var pkItemOrderVal = $(trNode.find("[name='pkItemOrder']")[0]).val();
			var pkWebItemOrderVal = $(trNode.find("[name='pkWebItemOrder']")[0]).val();
			var contractNoVal = $(trNode.find("[name='contractNo']")[0]).val();
			var pkProdVal = $(trNode.find("[name='pkProd']")[0]).val();
			var prodNameVal = $(trNode.find("[name='prodName']")[0]).val();
			var moduleVal = $(trNode.find("[name='module']")[0]).val();
			var typeVal = $(trNode.find("[name='type']")[0]).val();
			var descVal = $(trNode.find("[name='desc']")[0]).val();
			var batchNoVal = $(trNode.find("[name='batchNo']")[0]).val();
			var pkMeasureVal = $(trNode.find("[name='pkMeasure']")[0]).val();
			var measureNameVal = $(trNode.find("[name='measureName']")[0]).val();
			var orderNumVal = $(trNode.find("[name='orderNum']")[0]).val();
			var orderPriceVal = $(trNode.find("[name='orderPrice']")[0]).val();
			var discountVal = $(trNode.find("[name='discount']")[0]).val();
			var netTaxPriceVal = $(trNode.find("[name='netTaxPrice']")[0]).val();
			var priceTaxTotalVal = $(trNode.find("[name='priceTaxTotal']")[0]).val();
			var rateVal = $(trNode.find("[name='rate']")[0]).val();
			var taxNumVal = $(trNode.find("[name='taxNum']")[0]).val();
			var pkCurrencyVal = $(trNode.find("[name='pkCurrency']")[0]).val();
			var curNameVal = $(trNode.find("[name='curName']")[0]).val();
			var planArrDateVal = $(trNode.find("[name='planArrDate']")[0]).val();
			var pkTransferTypeVal = $(trNode.find("[name='pkTransferType']")[0]).val();
			var transferNameVal = $(trNode.find("[name='transferName']")[0]).val();
			var pkReceiveAddrVal = $(trNode.find("[name='pkReceiveAddr']")[0]).val();
			var receiveAddrVal = $(trNode.find("[name='receiveAddr']")[0]).val();
			var pkProducerVal = $(trNode.find("[name='pkProducer']")[0]).val();
			var produceFactoryVal = $(trNode.find("[name='produceFactory']")[0]).val();
			var pkQulityLevelVal = $(trNode.find("[name='pkQulityLevel']")[0]).val();
			var qulityLevelVal = $(trNode.find("[name='qulityLevel']")[0]).val();
			var taxPriceVal = $(trNode.find("[name='taxPrice']")[0]).val();
			var numScaleVal = $(trNode.find("[name='numScale']")[0]).val();
			var priceScaleVal = $(trNode.find("[name='priceScale']")[0]).val();
			var amountScaleVal = $(trNode.find("[name='amountScale']")[0]).val();
			var maintaxfreepriceVal = $(trNode.find("[name='maintaxfreeprice']")[0]).val();
			var maintaxpriceVal = $(trNode.find("[name='maintaxprice']")[0]).val();
			var confirmPriceVal = $(trNode.find("[name='confirmPrice']")[0]).val();
			var confirmNumVal = $(trNode.find("[name='confirmNum']")[0]).val();
			var planArrDateddVal = $(trNode.find("[name='planArrDatedd']")[0]).val();
			var confirmArrDateVal = $(trNode.find("[name='confirmArrDate']")[0]).val();
			
			var tempStr = "&pkItemOrder=" + pkItemOrderVal
						  +"%26" + pkWebItemOrderVal 
						  +"%26" + contractNoVal
			              +"%26" + pkProdVal 
			              +"%26" + "" 
			              +"%26" + "" 
			              +"%26" + "" 
			              +"%26" + descVal 
			              +"%26" + batchNoVal 
			              +"%26" + pkMeasureVal 
			              +"%26" + measureNameVal 
			              +"%26" + orderNumVal 
			              +"%26" + orderPriceVal 
			              +"%26" + discountVal 
			              +"%26" + netTaxPriceVal 
			              +"%26" + priceTaxTotalVal 
			              +"%26" + rateVal 
			              +"%26" + taxNumVal 
			              +"%26" + pkCurrencyVal 
			              +"%26" + curNameVal 
			              +"%26" + planArrDateVal 
			              +"%26" + pkTransferTypeVal 
			              +"%26" + transferNameVal 
			              +"%26" + pkReceiveAddrVal 
			              +"%26" + receiveAddrVal 
			              +"%26" + pkProducerVal 
			              +"%26" + produceFactoryVal 
			              +"%26" + pkQulityLevelVal 
			              +"%26" + qulityLevelVal 
			              +"%26" + taxPriceVal 
			              +"%26" + numScaleVal 
			              +"%26" + priceScaleVal 
			              +"%26" + amountScaleVal 
			              +"%26" + maintaxfreepriceVal 
			              +"%26" + maintaxpriceVal 
			              +"%26" + confirmPriceVal
			              +"%26" + confirmNumVal
			              +"%26" + planArrDateddVal
			              +"%26" + confirmArrDateVal;
			formStr1 += tempStr;
		});
		
		//这里改成ajax提交
		//$("#orderForm").attr("action", ebvp_context + "/order/cmtUptOrderOnSave");
		//$("#orderForm").submit();
		
		//ajax提交
		var bodyContent = $.ajax( {
			url : ebvp_context + "/order/cmtUptOrderOnSave",
			type : "POST",
			data : formStr1,
			dataType : "text",
			async : false,
			success : function(responseText) {
				$("body").find(".block1").remove();
				if(responseText!= null && responseText.indexOf("-@BUSIERROR@-") != -1){
					if(responseText.indexOf("TS_CHECKERROR") != -1){
						responseText = "-@BUSIERROR@-" + "数据已修改，请刷新页面重新操作";
					}
					var spanObj = $("<span></span>").attr("style", "float:right; color:red;").html(responseText.substring(13,responseText.length));
					var imgObj = $("<img></img>").attr("style", "float:right;").attr("src", "/ebvp/commonres/common/frame/src/img/img_error.png");
					var div = $("<div></div>").addClass("block1").attr("style","float:right;");
					div.append(spanObj).append(imgObj);
					div.insertAfter($(".block2"));
				}else if(responseText!= null && responseText.indexOf("-@SYSERROR@-") != -1){
					var spanObj = $("<span></span>").attr("style", "float:right; color:red;").html(responseText.substring(12,responseText.length));
					var imgObj = $("<img></img>").attr("style", "float:right;").attr("src", "/ebvp/commonres/common/frame/src/img/img_error.png");
					var div = $("<div></div>").addClass("block1").attr("style","float:right;");
					div.append(spanObj).append(imgObj);
					div.insertAfter($(".block2"));
				}else{
					//这里的pkWebOrder无法从页面拿到，则传一个标记AJAXREFRESH，后台查询服务里，根据这个标记判断是否是ajax提交后刷新，还是正常的浏览，从而改变查询方法
					location.href = ebvp_context + "/order/browseOrder?orderPk="+$("#pkOrder").val()+"&pkWebOrder=AJAXREFRESH&qryconfirmStatus=5";
					//location.href = ebvp_context + "/order/browseOrder?orderPk="+$("#pkOrder").val()+"&pkWebOrder="+$("#pkWebOrder").val()+"&qryconfirmStatus=5";
				}
			}
		})
	});

	
	$(".numType").bind('blur', function() {
		validatorData($(this));
	});
	
	$("#confirmDetail_cancel").bind('click', function(){
		if($("#pkWebOrder").val() != null && $("#pkWebOrder").val()!=""){
			location.href = ebvp_context + "/order/deleteSavedOrder?pkWebOrder=" + $("#pkWebOrder").val() + "&pkOrder=" + $("#pkOrder").val();
		}else {
			location.href = ebvp_context + "/order/deleteSavedOrder?pkWebOrder=AFTERAJAXSAVE&pkOrder=" + $("#pkOrder").val();
		}
	});
	
	$("[name='confirmNum']").bind('change', function() {
		if(1!=1){
			var numScale = $($(this).parents("tr").find("[name='numScale']")[0]).val();
			subReduNum($(this), numScale);
			cal("amount", $(this));
		}
	});
	
	$("[name='confirmPrice']").bind('change', function() {
		var priceScale = $($(this).parents("tr").find("[name='priceScale']")[0]).val();
		subReduNum($(this), priceScale);
		cal("has_tax_price", $(this));
	});
	
	//日期发生变更时，校验生效日期和失效日期的大小
	window.onDatePickerSelect = function(obj){
		var formRow = $(obj).parents("tr");
		var vDate = $("#Text8").val().replace("-", "/").replace("-","/");
		var invDate = $(formRow.find("input[name='confirmArrDate']")[0]).val().replace("-", "/").replace("-","/");
		var date1 = new Date(Date.parse(vDate));
		var date2 = new Date(Date.parse(invDate));
				
		formRow.find("[for='confirmArrDate']").remove();
		if(date1 > date2){
			var dateErrMsg = getLanMsg("0ec22230-100090");
			if(formRow.find("[for='confirmArrDate']").length == 0){
				var errLabel = $("<img></img>").addClass("error").attr("for", "confirmArrDate").attr("title", dateErrMsg)
					.attr("src", "/ebvp/commonres/common/frame/src/img/validate_error.png");
				errLabel.insertAfter($(obj));
			}
			return;
		}
	};
	
	
	
	// 截去多余的小数位数或者补足小数位
	function subReduNum(obj, intPrecision) {
		var strValue = obj.val();
		if (isNaN(strValue)) {
			return;
		}
		if (strValue == null || strValue == "") {
			return;
		}
		if (strValue.indexOf(".") != -1) {
			var values = strValue.split(".");
			if (values[1].length > intPrecision) {
				if (intPrecision > 0) {
					strValue = values[0] + "."
							+ values[1].substring(0, intPrecision);
					strValue = roundNum(strValue, values[1]
							.substring(intPrecision), intPrecision);
				} else {
					strValue = values[0];
				}
			} else {
				for ( var i = values[1].length; i < intPrecision; i++) {
					strValue += "0";
				}
			}
		} else {
			if (intPrecision > 0) {
				strValue += ".";
				for ( var i = 0; i < intPrecision; i++) {
					strValue += "0";
				}
			}
		}
		obj.val(strValue);
	}
	
	//处理计算
	function cal(changeKey, obj){
		// 计算行号
		var trObj = obj.parent().parent();
		
		// 物料主健
		$("#pk_prod_cal").val( trObj.find("[name='pkProd']").val());
		// 报价单位
		$("#pk_unit_cal").val(trObj.find("[name='pkMeasure']").val());
		// 币种
		$("#pk_currency_cal").val(trObj.find("[name='pkCurrency']").val()) ;
		// 币种名称
		$("#curName_cal").val(trObj.find("[name='curName']").val());
		// 确认数量
		// 主数量
		var newamounts = $(trObj.find("[name='confirmNum']")[0]).val();
		if($(trObj.find("[name='confirmNum']")[0]).val() == null || $(trObj.find("[name='confirmNum']")[0]).val()==""){
			newamounts = 0;
		}
		if(!($(trObj.find("[name='taxNum']")[0]).val() == null || $(trObj.find("[name='taxNum']")[0]).val()=="")){
			newamounts = newamounts * $(trObj.find("[name='taxNum']")[0]).val();
		}
		$("#amount_cal").val(newamounts);
		$("#primary_amount_cal").val(newamounts);
		
		// 确认含税单价
		if($(trObj.find("[name='confirmPrice']")[0]).val() == null || $(trObj.find("[name='confirmPrice']")[0]).val()==""){
			// 含税净价
			$(trObj.find("[title='netTaxPrice_span']")[0]).html($(trObj.find("[name='confirmPrice']")[0]).val());
			return ;	
		}
		$("#has_tax_price_cal").val($(trObj.find("[name='confirmPrice']")[0]).val());
		
		// 含税净价
		$("#has_tax_net_price_cal").val($(trObj.find("[title='netTaxPrice_span']")[0]).html());
		$("#conversion_rate_cal").val("1/1");
		
		// 税率
		if($(trObj.find("[title='rate_span']")[0]).text() == "" || $(trObj.find("[title='rate_span']")[0]).text() == null){
			$(trObj.find("[title='rate_span']")[0]).text("0.00");
		}
		$("#tax_rate_cal").val($(trObj.find("[title='rate_span']")[0]).html());
		
		// 折扣率
		if($(trObj.find("[title='discount_span']")[0]).text() == "" || $(trObj.find("[title='discount_span']")[0]).text() == null){
			return;
		}
		$("#discount_rate_cal").val($(trObj.find("[title='discount_span']")[0]).html());
		
		// lilongc添加，mawl注释的;
		// 如需修改与 lilongc, mawl沟通一下
		//主无税净价
		//if($(trObj.find("[name='maintaxfreeprice']")[0]).val() == null || $(trObj.find("[name='maintaxfreeprice']")[0]).val()==""){
		//	return ;
		//}
		//$("#primary_notax_net_price_cal").val($(trObj.find("[name='maintaxfreeprice']")[0]).val());
		//主含税净价
		//if($(trObj.find("[name='maintaxprice']")[0]).val() == null || $(trObj.find("[name='maintaxprice']")[0]).val()==""){
		//	return ;
		//}
		//$("#primary_hastax_net_price_cal").val($(trObj.find("[name='maintaxprice']")[0]).val());
		
		// 触发计算的字段为: changeKey
		$("#property_cal").val(changeKey);
		
		// 提交表单
		var options2 = {
			beforeSubmit : showRequest2, 
			success : showResponse2
		};
		
		var formStr = $('#calpriceform').serialize();
		var bodyContent = $.ajax( {
			url : ebvp_context + "/sourcing/biddingprojlist/calprice",
			type : "POST",
			data : "&" + formStr,
			dataType : "text",
			async : false,
			success : function(responseText) {
				//alert(responseText);
				showResponse2(responseText, obj);
			}
		})
	}
		
	function showRequest2(formData, jqForm, options) {
	}
		
	function showResponse2(responseText, obj) {
		// 0:报价数量|1:税率|2:折扣率|3:含税单价|4:价税合计|5:含税净价|6:主含税净价|7:无税单价|8:无税金额|9:无税净价|10:主无税净价|11:税e|12:行号|13:换算率|14:主数量
		var responseArr = responseText.split("|");
		
		var trObj = obj.parent().parent();
		if(responseArr[4] == 0.00 || responseArr[4] == "null"){
			responseArr[4] = "";
		}
		//价税合计
		$(trObj.find("[name='priceTaxTotal']")[0]).val(responseArr[4]);
		var amountScale = $(trObj.find("[name='amountScale']")[0]).val();
		subReduNum($(trObj.find("[name='priceTaxTotal']")[0]), amountScale);
		$(trObj.find("[title='priceTaxTotal_span']")[0]).html($(trObj.find("[name='priceTaxTotal']")[0]).val());
		
		//净含税单价
		if(responseArr[5] == 0.00 || responseArr[5] == "null"){
			responseArr[5] = "";
		}
		$(trObj.find("[name='netTaxPrice']")[0]).val(responseArr[5]);
		var priceScale = $(trObj.find("[name='priceScale']")[0]).val();
		subReduNum($(trObj.find("[name='netTaxPrice']")[0]), priceScale);
		$(trObj.find("[title='netTaxPrice_span']")[0]).html($(trObj.find("[name='netTaxPrice']")[0]).val());
		
		//主无税净价
		if(responseArr[9] == 0.00 || responseArr[9] == "null"){
			responseArr[9] = "";
		}
		$(trObj.find("[name='maintaxfreeprice']")[0]).val(responseArr[9]);
//		var amountScale = $(trObj.find("[name='amountScale']")[0]).val();
//		subReduNum($(trObj.find("[name='maintaxfreeprice']")[0]), amountScale);
//		$(trObj.find("[title='maintaxfreeprice']")[0]).html($(trObj.find("[name='maintaxfreeprice']")[0]).val());
		
		//主含税净价
		if(responseArr[5] == 0.00 || responseArr[5] == "null"){
			responseArr[5] = "";
		}
		$(trObj.find("[name='maintaxprice']")[0]).val(responseArr[5]);
//		var priceScale = $(trObj.find("[name='priceScale']")[0]).val();
//		subReduNum($(trObj.find("[name='netTaxPrice']")[0]), priceScale);
//		$(trObj.find("[title='netTaxPrice_span']")[0]).html($(trObj.find("[name='netTaxPrice']")[0]).val());
	}
	
	// trim
	function trim(strToTrim) {
		
		if(strToTrim == undefined){
			return;
		}
		
		result = strToTrim;
		for (_i = 0; _i < strToTrim.length; _i++) {
			if (strToTrim.charAt(_i) == " ")
				result = strToTrim.substr(_i + 1)
			else
				_i = strToTrim.length;
		}
		strToTrim = result;
		for (_i = strToTrim.length - 1; _i >= 0; _i--) {
			if (strToTrim.charAt(_i) == " ")
				result = strToTrim.substr(0, _i);
			else
				_i = 0;
		}
		return result;
	}
	
	function validatorData(obj) {
		// 先把非数字的都替换掉，除了数字和.
		obj.val(obj.val().replace(/[^\d.]/g, ""));
		// 必须保证第一个为数字而不是.
		obj.val(obj.val().replace(/^\./g, ""));
		// 保证只有出现一个.而没有多个.
		obj.val(obj.val().replace(/\.{2,}/g, "."));
		// 保证.只出现一次，而不能出现两次以上
		obj.val(obj.val().replace(".", "$#$").replace(/\./g, "").replace("$#$",
				"."));
		if (parseFloat(obj.val()) <= 0) {
			obj.val(1);
		}
		if (obj.val() != null && obj.val() != undefined && obj.val() != ""
				&& parseFloat(obj.val()) >= 0) {
			return true;
		} else {
			obj.val(1);
			return false;
		}
	}
});	

$(function() {
	
	var menuGroups = [ {
		'menuId' : "conntsMenu",
		'menuTitle' : getLanMsg("0ec22230-100079"),
		'iconClass' : "account",
		'iconImg' : null,
		'needShow': bModuleEBPOrSCM_PU,
		'items' : [ {
			'itemId' : "tenderMenuItem1",
			'itemUrl' : "/ebvp/order/confirmlist?menuid=31",
			'itemName' : getLanMsg("0ec22230-100080"),
			'needShow': bModuleEBPOrSCM_PU,
			'callback': function() { window.location = ebvp_context + "/order/confirmlist?menuid=31"; return false; }
		}, {
			'itemId' : "tenderMenuItem2",
			'itemUrl' : "/ebvp/order/exestatuslist?menuid=32",
			'itemName' : getLanMsg("0ec22230-100081"),
			'needShow': bModuleSCM_PU && bStdVersion,
			'callback': function() { window.location = ebvp_context + "/order/exestatuslist?menuid=32"; return false; }
		}, {
			'itemId' : "tenderMenuItem3",
			'itemUrl' : "/ebvp/order/exeinfolist?menuid=33",
			'itemName' : getLanMsg("0ec22230-100082"),
			'needShow': bModuleSCM_PU,
			'callback': function() { window.location = ebvp_context + "/order/exeinfolist?menuid=33"; return false; }
		} ]
	} ];

	/* 左侧菜单栏 */
	$("#columnLeft").UIColumnMenu( {
		'menuGroups' : menuGroups,
		'menuHeight' : heigth,
		'singleGroup' : true
	});
	
	$(function() {	
		$( "#tenderMenuItem1" ).parents("ul").find(".current").removeClass("current");
		$( "#tenderMenuItem1" ).addClass("current");
	});
	
	
});